//
//  LoginCodeViewController.swift
//  PandaSwift
//
//  Created by 余意 on 2017/1/17.
//  Copyright © 2017年 余意. All rights reserved.
//

import UIKit
import SnapKit
import Alamofire
import PKHUD

class LoginCodeViewController: UIViewController {
    
    let containerSV = UIScrollView()
    let containerView = UIView()
    let headIV = UIImageView()
    let whiteView = UIView()
    let mobileTF = UITextField()
    let codeTF = UITextField()
    let codeBtn = UIButton()
    let loginBtn = UIButton()
    let registerBtn = UIButton()
    let passwordBtn = UIButton()
    
    var mobile =  String()
    var code = String()
    
    var countdownTimer: Timer?
    
    var remainingSeconds:Int = 0 {
        willSet{
            codeBtn.setTitle("剩余\(newValue)s", for: .normal)
            
            if newValue <= 0
            {
                codeBtn.setTitle("获取验证码", for: .normal)
                isCounting = false
            }
        }
    }
    
    var isCounting = false{
        willSet{
            if newValue{
                countdownTimer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (timer) in
                    self.remainingSeconds -= 1
                })
                
                remainingSeconds = 9
                codeBtn.backgroundColor = UIColor.gray
            }else{
                countdownTimer?.invalidate()
                countdownTimer = nil
                codeBtn.backgroundColor = UIColor.red
            }
            
            codeBtn.isEnabled = !newValue
        }
    }
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.title = "登录"
        self.view.backgroundColor = BACKGROUND_COLOR
        
        initUI()
        
        mobileTF.text = "18521090832"
        
        HUD.flash(.label("loading"), delay: 3.0)
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func codeBtnClick(sender:UIButton)
    {
        mobile = mobileTF.text!
        
        if mobile.isEmpty
        {
            return;
        }
        else
        {
            let urlStr = "http://121.40.121.237:4400/api/captcha?mobile=\(mobile)"
            
            NetworkTools.requestData(.get, URLString: urlStr) { (result) in
                print(result)
                self.isCounting = true
            }
        }
    }
    
    func loginBtnClick(sender:UIButton)
    {
        self.navigationController?.dismiss(animated: true, completion: nil)
    }
    
    func registerBtnClick(sender:UIButton)
    {
        let RVC = RegisterViewController()
        self.navigationController?.pushViewController(RVC, animated: true)
    }
    
    func passwordBtnClick(sender:UIButton)
    {
        let LPVC = LoginPasswordViewController()
        self.navigationController?.pushViewController(LPVC, animated: true)
    }
    
    private func initUI()
    {
        self.view.addSubview(containerSV)
        containerSV.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view).inset(UIEdgeInsetsMake(0, 0, 0, 0))
        }
        
        containerSV.addSubview(containerView)
        containerView.snp.makeConstraints { (make) in
            make.edges.equalTo(containerSV)
            make.width.equalTo(containerSV)
        }
        
        headIV.layer.cornerRadius = 40
        headIV.layer.masksToBounds = true
        headIV.backgroundColor = UIColor.red
        containerView.addSubview(headIV)
        headIV.snp.makeConstraints { (make) in
            make.top.equalTo(containerView).offset(16)
            make.size.equalTo(CGSize(width:80,height:80))
            make.centerX.equalTo(containerView)
        }
        
        whiteView.backgroundColor = UIColor.white
        whiteView.layer.cornerRadius = 4;
        whiteView.layer.masksToBounds = true
        containerView.addSubview(whiteView)
        whiteView.snp.makeConstraints { (make) in
            make.top.equalTo(headIV.snp.bottom).offset(12)
            make.left.equalTo(containerView).offset(8)
            make.right.equalTo(containerView).offset(-8)
            make.height.equalTo(100.5)
        }
        
        let view = UIView()
        whiteView.addSubview(view)
        view.snp.makeConstraints { (make) in
            make.top.left.right.equalTo(whiteView);
            make.height.equalTo(50)
        }
        
        let mobileLab = UILabel()
        mobileLab.text = "用户名"
        mobileLab.textColor = TEXT_BLACK_COLOR
        mobileLab.font = UIFont.systemFont(ofSize: 16)
        whiteView.addSubview(mobileLab)
        mobileLab.snp.makeConstraints { (make) in
            make.left.equalTo(whiteView).offset(16)
            make.centerY.equalTo(view)
        }
        
        let mobileline = UIImageView()
        mobileline.image = UIImage(named:"line_vertical")
        whiteView.addSubview(mobileline)
        mobileline.snp.makeConstraints { (make) in
            make.left.equalTo(whiteView).offset(75)
            make.centerY.equalTo(view)
            make.top.equalTo(view).offset(8)
            make.width.equalTo(0.5)
        }
        
        whiteView.addSubview(mobileTF)
        mobileTF.keyboardType = .numberPad
        mobileTF.clearButtonMode = .whileEditing
        mobileTF.placeholder = "请输入手机号"
        mobileTF.font = UIFont.systemFont(ofSize: 14)
        mobileTF.snp.makeConstraints { (make) in
            make.left.equalTo(whiteView).offset(85)
            make.right.equalTo(whiteView);
            make.centerY.equalTo(view)
        }
        
        let longline = UIImageView()
        longline.image = UIImage(named:"line_gray")
        whiteView.addSubview(longline)
        longline.snp.makeConstraints { (make) in
            make.top.equalTo(whiteView).offset(50)
            make.left.right.equalTo(whiteView)
            make.height.equalTo(0.5)
        }
        
        let codeLab = UILabel()
        codeLab.text = "验证码"
        codeLab.font = UIFont.systemFont(ofSize: 16)
        codeLab.textColor = TEXT_BLACK_COLOR
        whiteView.addSubview(codeLab)
        codeLab.snp.makeConstraints { (make) in
            make.centerY.equalTo(mobileLab).offset(50.5)
            make.left.equalTo(mobileLab)
        }
        
        let codeline = UIImageView()
        codeline.image = UIImage(named:"line_vertical")
        whiteView.addSubview(codeline)
        codeline.snp.makeConstraints { (make) in
            make.centerY.equalTo(mobileline).offset(50.5)
            make.centerX.equalTo(mobileline)
            make.size.equalTo(mobileline)
        }
        
        codeBtn.backgroundColor = UIColor.red
        codeBtn.layer.cornerRadius = 4
        codeBtn.setTitle("获取验证码", for: .normal)
        codeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        codeBtn.setTitleColor(UIColor.white, for: .normal)
        codeBtn.addTarget(self, action: #selector(codeBtnClick(sender:)), for: .touchUpInside)
        whiteView.addSubview(codeBtn)
        codeBtn.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize(width:80, height:32))
            make.right.equalTo(whiteView).offset(-8)
            make.centerY.equalTo(codeLab)
        }
        
        whiteView.addSubview(codeTF)
        codeTF.clearButtonMode = .whileEditing
        codeTF.placeholder = "请输入验证码"
        codeTF.font = UIFont.systemFont(ofSize: 14)
        codeTF.snp.makeConstraints { (make) in
            make.left.equalTo(mobileTF)
            make.right.equalTo(codeBtn.snp.left);
            make.centerY.equalTo(codeLab)
        }
        
        containerView.addSubview(loginBtn)
        loginBtn.addTarget(self, action: #selector(loginBtnClick(sender:)), for: .touchUpInside)
        loginBtn.setBackgroundImage(UIImage(named:"defalut_btn"), for: .normal)
        loginBtn.setTitle("登录", for: .normal)
        loginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 17)
        loginBtn.snp.makeConstraints { (make) in
            make.top.equalTo(whiteView.snp.bottom).offset(18)
            make.left.equalTo(containerView).offset(16)
            make.right.equalTo(containerView).offset(-16)
            make.height.equalTo(45)
        }
        
        let registerLab = UILabel()
        registerLab.text = "注册新用户"
        registerLab.textColor = TEXT_RED_COLOR
        registerLab.font = UIFont.systemFont(ofSize: 14)
        containerView.addSubview(registerLab)
        registerLab.snp.makeConstraints { (make) in
            make.top.equalTo(loginBtn.snp.bottom).offset(4)
            make.left.equalTo(containerView).offset(16)
        }
        
        containerView.addSubview(registerBtn)
        registerBtn.addTarget(self, action: #selector(registerBtnClick(sender:)), for: .touchUpInside)
        registerBtn.snp.makeConstraints { (make) in
            make.edges.equalTo(registerLab).inset(UIEdgeInsetsMake(-2, -2, 2, 2))
        }
        
        let passwordLab = UILabel()
        passwordLab.text = "使用验证码登录"
        passwordLab.textColor = TEXT_RED_COLOR
        passwordLab.font = UIFont.systemFont(ofSize: 14)
        containerView.addSubview(passwordLab)
        passwordLab.snp.makeConstraints { (make) in
            make.top.equalTo(loginBtn.snp.bottom).offset(4)
            make.right.equalTo(containerView).offset(-16)
        }
        
        containerView.addSubview(passwordBtn)
        passwordBtn.addTarget(self, action: #selector(passwordBtnClick(sender:)), for: .touchUpInside)
        passwordBtn.snp.makeConstraints { (make) in
            make.edges.equalTo(passwordLab).inset(UIEdgeInsetsMake(-2, -2, 2, 2))
            make.bottom.equalTo(containerView).offset(-20)
        }
        
        
    }
}
